package com.matt.system.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.matt.system.dao.UserDao;
import com.matt.system.domain.TSysuser;
@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
	
	@SuppressWarnings("unchecked")
	public List<TSysuser> findAll(){
		return this.getHibernateTemplate().find("from TSysuser");
	};
	
	@SuppressWarnings("unchecked")
	public TSysuser findByCode(String code){
		TSysuser user = null;
		List list = this.getHibernateTemplate().find("from TSysuser t where t.TSuCode =?", code);
		if(list.size() == 0){
			return null;
		}
		user = (TSysuser) list.get(0);
		return user;
	};
	
	public void addUser(TSysuser user){
		this.getHibernateTemplate().saveOrUpdate(user);
	};
	
	public void delUser(TSysuser user){
		
	};
	
	public void updateUser(TSysuser user){
		
	};
	
	@SuppressWarnings("unchecked")
	public TSysuser findByNameAndPassword(String name,String password){
		TSysuser user = null;
		List list = this.getHibernateTemplate().find("from TSysuser t where t.TSuCode =? and t.TSuPassword = ? ", name,password);
		if(list.size() == 0){
			return null;
		}
		user = (TSysuser) list.get(0);
		return user;
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TSysuser> findAllByPage(final int pageNow, final int pageSize) {
		List list = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Query q = arg0.createQuery("from TSysuser order by TSuName");
						q.setFirstResult(pageNow);
						q.setMaxResults(pageSize);
						List users = q.list();
						return users;
					}
				});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public long getRows() {
		List list = this.getHibernateTemplate().find("select count(*) from TSysuser");
		return (Long) list.iterator().next();
	}

	@SuppressWarnings("unchecked")
	public List<TSysuser> findPageByCriteria(final int pageNow,final int pageSize,final DetachedCriteria detachedCriteria ) {
		List list = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = detachedCriteria.getExecutableCriteria(arg0);
						criteria.setProjection(null);
						criteria.setFirstResult(pageNow);
						criteria.setMaxResults(pageSize);
						return criteria.list();
					}
				});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public long getCountByCriteria(final DetachedCriteria detachedCriteria) {
		Long count = getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				Criteria criteria = detachedCriteria.getExecutableCriteria(arg0);
				criteria.setProjection(null);
				return criteria.setProjection(Projections.rowCount()).uniqueResult();
			}
		});
		return count.longValue();
	};
}
